import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { userStores } from './user'
import { insertCartAPI, carListApi, delCartAPI } from '@/apis/car'

export const carStores = defineStore(
  'car',
  () => {
    //user.js 中token

    const getUserSrore = userStores()
    const isLogin = computed(() => getUserSrore.userInfo.token)

    const carList = ref([])

    //添加购物车
    const addCart = async (goods) => {
      //判断传递过来的对象中skuid是否存在，如果存在就是添加过 之间count++
      //如果不存在就直  carList.push()
      //从goods对象中解构需要传递的参数
      const { skuId, count } = goods
      // 登录
      if (isLogin.value) {
        // 登录之后的加入购车逻辑
        await insertCartAPI({ skuId, count })

        getNewList()
      } else {
        const item = carList.value.find((item) => goods.skuId === item.skuId)
        // console.log(item)
        if (item) {
          item.count++
        } else {
          carList.value.push(goods)
        }
      }
    }

    //删除购物车

    const delCart = async (skuId) => {
      //1.找到要删除项的下标  splice  findIndex()

      // const index = carList.value.findIndex((item) => skuId === item.skuId)

      // carList.value.splice(index, 1)

      //2.filter 过滤

      if (isLogin.value) {
        //登录之后的逻辑
        await delCartAPI([skuId])
        getNewList()
      } else {
        //登录之前的逻辑
        carList.value = carList.value.filter((item) => skuId !== item.skuId)
        // console.log(carList.value)
      }
    }

    const getNewList = async () => {
      const { result } = await carListApi()
      // console.log(res)
      carList.value = result
    }

    //统计
    //商品总数 reduce()  所有商品的count累加
    //商品价格 conut*pirce
    // const allCount = computed(() => {
    //   return carList.value.reduce((a, c) => {
    //     return (a += c.count)
    //   }, 0)
    // })

    //简写
    // 总数量
    const allCount = computed(() =>
      carList.value.reduce((a, b) => (a += b.count), 0)
    )
    //总价格
    const allPrice = computed(() =>
      carList.value.reduce((a, b) => (a += b.count * b.price), 0)
    )

    // 本地单选同步Pinia功能

    const singleCheck = (skuId, selected) => {
      // 通过skuId找到要修改的那一项 然后把它的selected修改为传过来的selected

      const item = carList.value.find((item) => skuId === item.skuId)

      item.selected = selected
    }

    //全选控制单选
    const allCheck = (selected) => {
      carList.value.forEach((item) => (item.selected = selected))
    }

    //单选控制全选

    const isAll = computed(() => carList.value.every((item) => item.selected))

    //已选择数量
    const selectedCount = computed(() =>
      carList.value
        .filter((item) => item.selected)
        .reduce((a, c) => (a += c.count), 0)
    )

    //已选商品总和
    const selectedPrice = computed(() =>
      carList.value
        .filter((item) => item.selected)
        .reduce((a, c) => (a += c.count * c.price), 0)
    )

    //清除购物车
    const clearCar = () => {
      carList.value = []
    }

    return {
      carList,
      addCart,
      delCart,
      clearCar,
      allCount,
      allPrice,
      singleCheck,
      isAll,
      allCheck,
      selectedCount,
      selectedPrice,
      getNewList
    }
  },
  {
    persist: true
  }
)
